﻿using System;
using System.Reflection;
using EPiServer.DataAbstraction;
using EPiSugar.EntityAdapter.PropertyBinders;

namespace EPiSugar.EntityAdapter
{
    /// <summary>
    /// Binds the entity property to a named page data property. By default page data properties are bound to an element's property with the same name if it exists.
    /// </summary>
    public class BindToPropertyAttribute : Attribute, IBinderFactory
    {
        private readonly string propertyName;

        /// <summary>
        /// Select the page data property name to use for this property.
        /// </summary>
        /// <param name="propertyName">The name of the page data property as defined in admin mode.</param>
        public BindToPropertyAttribute(string propertyName)
        {
            this.propertyName = propertyName;
        }

        public IPropertyBinder CreateBinder(PageDefinitionCollection propertyDefinitions, PropertyInfo destinationProperty)
        {
            return new PropertyBinder(propertyName, destinationProperty);
        }
    }
}
